package com.nstar.base.lang
{
	import mx.collections.ArrayCollection;
	import flash.errors.StackOverflowError;
	
	/**
	 * 
	 * 栈数据结构
	 * 可任意push,容量在初始化的时候限定，当容量到上限后，栈底元素出栈
	 * @author 李嘉陵
	 */
	public class Stack
	{
		public var array:Array;
		private	var length:int = 10; //栈可容纳的深度
		public var index:int = -1; //堆栈到第几位
		private var _size:int = 0; //栈实际深度
		
		public function Stack(length:int = 10) {
			array = new Array(length);
			this.length = length;
		}
		
		public function push(obj:*):void {
			index++;
			if(index > length-1) {
				index = 0;
			}
			array[index] = obj;
			
			if(_size < length)
				_size++;
		}
		
		public function pop():* {
			
			if(index == -1) {
				index = length-1;
			}
			index--;
			
			_size--;
			if(_size == -1) {
				array = new Array(length);
				index = -1;
				_size = 0;
				throw new StackOverflowError("栈空，无法pop");
			}
				
			
			
			return array[index+1];
		}
		
		public function isEmpty():Boolean {
			return (_size == 0);
		}
		
		public function isNotEmpty():Boolean {
			return (_size != 0);
		}
		
		public function get size():int {
			return _size;
		}
	}
}